<?php
namespace App\Http\Controllers\Xcx\Ywmall;

use App\Service\Xcx\Ywmall\GoodsService;
use App\Utility\Upload;
use Illuminate\Http\Request;

class GoodsController extends BaseController
{
    //商品

    //列表
    public function index(Request $request)
    {
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 6);
        $pas['uid'] = $request->post('uid', 0);
        $pas['cate_id'] = $request->post('cate_id', 0);
        $pas['shequ_id'] = $request->post('shequ_id', 0);
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        $res = GoodsService::index($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function one(Request $request)
    {
        $code = $request->post('code', '');
        $res = GoodsService::oneidByCode($code);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = GoodsService::onebyid($res['data']);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //详情
    public function onebyid(Request $request)
    {
        $id = $request->post('id', 0);
        $res = GoodsService::onebyid($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //详情
    public function onebyserial(Request $request)
    {
        $serial = $request->get('show_code', '');
        $res = GoodsService::codeBySerial($serial);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $type = $res['data']['yw_type'];
        $id = $res['data']['yw_id'];
        if ($type != 2) {
            return $this->toJson(-2, '类型错误');
        }
        $res = GoodsService::onebyid($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //新增
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $whe['shop_name'] = $request->post('shop_name', '');
        $whe['shequ_name'] = $request->post('shequ_name', '');
        $pas['uid'] = $request->post('uid', 0);
        $pas['code'] = $request->post('code', '');
        $pas['cover'] = $request->post('cover', '');
        $pas['gname'] = $request->post('gname', '');
        $pas['price'] = $request->post('price', 0) * 100;
        $pas['price_old'] = $request->post('price_old', 0) * 100;
        $pas['num'] = $request->post('num', 0);
        $pas['detail'] = $request->post('detail', '');
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
//        if (!$pas['code']) {
//            return $this->toJson(-2, '码必须的');
//        }
        if (!$pas['cover']) {
            return $this->toJson(-2, '图片必须的');
        }
        if (!$pas['gname']) {
            return $this->toJson(-2, '标题必须的');
        }
        if (strpos($pas['cover'], '.top/')) {
            $imgs = explode('.top/', $pas['cover']);
            $pas['cover'] = $imgs[1];
        }
        if ($id) {
            $res = GoodsService::modify($id, $pas, $whe);
        } else {
            $res = GoodsService::add($pas, $whe);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //图片上传
    public function imgUpload(Request $request)
    {
        $id = $request->post('tmpid', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $resUpload = Upload::uploadImg($request, 'img');
        if ($resUpload['code'] < 0) {
            return $this->toJson(-2, $resUpload['msg']);
        }
        $img = $resUpload['data'];
        $res = GoodsService::imgUpload($id, $img);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //分类列表
    public function cateList()
    {
        $res = GoodsService::cateList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //分类设置
    public function cateEdit(Request $request)
    {
        $tmp_id = $request->post('id', 0);
        $cate_id = $request->post('cate_id', 0);
        if (!$tmp_id) {
            return $this->toJson(-2, 'ID必须的');
        }
        if (!$cate_id) {
            return $this->toJson(-2, '分类必须的');
        }
        $res = GoodsService::cateEdit($tmp_id, $cate_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //属性列表
    public function attrList(Request $request)
    {
        $cate_id = $request->post('cate_id', 0);
        $tmp_id = $request->post('id', 0);
        if (!$tmp_id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = GoodsService::attrList($cate_id, $tmp_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //属性修改
    public function attrEdit(Request $request)
    {
        $tmp_id = $request->post('tmp_id', 0);
        $attr_id = $request->post('attr_id', 0);
        $attr_key = $request->post('attr_key', '');
        $attr_val = $request->post('attr_val', '');
        if (!$tmp_id) {
            return $this->toJson(-2, 'ID必须的');
        }
        if ($attr_id) {
            $res = GoodsService::attrEdit($attr_id, $attr_key, $attr_val);
        } else {
            $res = GoodsService::attrAdd($tmp_id, $attr_key, $attr_val);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //属性删除
    public function attrDel(Request $request)
    {
        $attr_id = $request->post('attr_id', 0);
        $res = GoodsService::attrDel($attr_id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}